package com.kinghood.productcenter.manager.impl;

import com.kinghood.infrastructure.model.KingHoodException;
import com.kinghood.infrastructure.model.constant.CommonConstants;
import com.kinghood.infrastructure.util.UserUtil;
import com.kinghood.productcenter.common.model.dao.base.BaseLabelDO;
import com.kinghood.productcenter.common.model.dao.base.BaseLabelItemDO;
import com.kinghood.productcenter.common.model.dao.product.ProductSkuLabelRelateDO;
import com.kinghood.productcenter.service.base.IBaseLabelItemService;
import com.kinghood.productcenter.service.base.IBaseLabelService;
import com.kinghood.productcenter.service.product.IProductSkuLabelRelateService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class BaseLabelManager {
    @Resource
    private IProductSkuLabelRelateService skuLabelRelateService;
    @Resource
    private IBaseLabelService labelService;
    @Resource
    private IBaseLabelItemService labelItemService;

    /**
     * 检查标签值是否被使用
     *
     * @param id id列表
     * @author jp
     * @date 2023/12/21
     */
    public void checkLabelItemUsed(Long id) {
        Long count = skuLabelRelateService.lambdaQuery().eq(ProductSkuLabelRelateDO::getLabelItemId, id)
                .eq(ProductSkuLabelRelateDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .count();
        if (count > 0) {
            throw new KingHoodException(-1, "当前标签值正在使用中，请删除使用商品后再重试");
        }
    }

    /**
     * 检查标签是否使用
     *
     * @param id 身份证件
     * @return
     * @author jp
     * @date 2024/01/12 15:02
     */
    public void checkLabelUsed(Long id) {
        Long count = skuLabelRelateService.lambdaQuery().in(ProductSkuLabelRelateDO::getLabelId, id)
                .eq(ProductSkuLabelRelateDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .count();
        if (count > 0) {
            throw new KingHoodException(-1, "当前标签正在使用中，请删除使用商品后再重试");
        }
    }

    /**
     * 删除标签
     *
     * @param id 身份证件
     * @return
     * @author jp
     * @date 2024/01/12 15:03
     */
    public void delLabelById(Long id) {
        BaseLabelDO label = labelService.getById(id);
        if (Objects.nonNull(label) && !label.getStatus().equals(CommonConstants.DISABLE)) {
            throw new KingHoodException(-1, "只有停用状态的才可操作删除");
        }
        // 判断标签值是否使用
        checkLabelUsed(id);
        labelService.lambdaUpdate().set(BaseLabelDO::getIsDelete, CommonConstants.IS_DELETE_Y)
                .set(BaseLabelDO::getUpdateTime, LocalDateTime.now())
                .set(BaseLabelDO::getUpdateUserId, UserUtil.getUserId())
                .eq(BaseLabelDO::getId, id)
                .eq(BaseLabelDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .update();
        labelItemService.lambdaUpdate().set(BaseLabelItemDO::getIsDelete, CommonConstants.IS_DELETE_Y)
                .set(BaseLabelItemDO::getUpdateTime, LocalDateTime.now())
                .set(BaseLabelItemDO::getUpdateUserId, UserUtil.getUserId())
                .eq(BaseLabelItemDO::getLabelId, id)
                .eq(BaseLabelItemDO::getIsDelete, CommonConstants.IS_DELETE_N)
                .update();
    }
}
